﻿Imports Controle
Imports Persistencia
Public Class frmTarefas
    Private dtDataBase As Date
    Private bSomenteAtrasados As Boolean
    Private prvIDCliente As Int32
    Private prvIDStatus As Int32
    Private objClientes As New ctrCliente
    Private objStatus As New ctrStatus
    Private objOcorrencias As New ctrOcorrencias
    Private objRegiao As New ctrRegiao
    Private objConversao As New perConversao

    Private dtClientes As New DataTable

    Private Enum eColTarefas

        eIdCliente = 0
        eNomeCliente = 1
        eIdContato = 2
        eNomeContato = 3
        eIDStatus = 4
        eStatus = 5
        eUltimoContato = 6
        eProximoContato = 7
        eHoraOculta = 8
        eHora = 9
        eTolerancia = 10
        eRegiao = 11

    End Enum

    Private Sub FormataDtGrid()
        Dim Colunas As New DataGridViewTextBoxColumn()
        Try
            With dtGridTarefas
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
                .AutoGenerateColumns = False
                .RowHeadersVisible = False

                .Columns.Clear()
                'define cada coluna do grid
                Dim Col As New DataGridViewTextBoxColumn()

                'Col.DataPropertyName = "idCliente"
                Col.HeaderText = "IDCliente"
                Col.Width = 15
                Col.Frozen = False
                Col.ReadOnly = True
                Col.FillWeight = 100
                Col.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                Col.SortMode = DataGridViewColumnSortMode.NotSortable
                Col.Visible = False

                .Columns.Add(Col)

                Dim Col1 As New DataGridViewTextBoxColumn()
                'Col1.DataPropertyName = "nomeCliente"
                Col1.HeaderText = "Cliente"
                Col1.Width = 160
                Col1.Frozen = False
                Col1.ReadOnly = True
                Col1.FillWeight = 100
                Col1.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                Col1.SortMode = DataGridViewColumnSortMode.NotSortable
                Col1.Visible = True

                .Columns.Add(Col1)

                Dim Col7 As New DataGridViewTextBoxColumn()
                'Col7.DataPropertyName = "IDContato"
                Col7.HeaderText = "IdContato"
                Col7.Width = 20
                Col7.Frozen = False
                Col7.ReadOnly = True
                Col7.FillWeight = 100
                Col7.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                Col7.SortMode = DataGridViewColumnSortMode.NotSortable
                Col7.Visible = False

                .Columns.Add(Col7)

                Dim Col8 As New DataGridViewTextBoxColumn()
                'Col8.DataPropertyName = "Responsavel"
                Col8.HeaderText = "Responsavel"
                Col8.Width = 70
                Col8.Frozen = False
                Col8.ReadOnly = True
                Col8.FillWeight = 100
                Col8.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                Col8.SortMode = DataGridViewColumnSortMode.NotSortable
                Col8.Visible = True

                .Columns.Add(Col8)

                Dim Col2 As New DataGridViewTextBoxColumn
                'Col2.DataPropertyName = "IDStatus"
                Col2.HeaderText = "IDStatus"
                Col2.Width = 70
                Col2.Frozen = False
                Col2.ReadOnly = True
                Col2.FillWeight = 100
                Col2.Visible = False
                Col2.SortMode = DataGridViewColumnSortMode.NotSortable
                .Columns.Add(Col2)

                Dim Col3 As New DataGridViewTextBoxColumn
                'Col3.DataPropertyName = "Status"
                Col3.HeaderText = "Status"
                Col3.Width = 50
                Col3.Frozen = False
                Col3.ReadOnly = True
                Col3.FillWeight = 100
                Col3.Visible = True
                Col3.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                Col3.SortMode = DataGridViewColumnSortMode.NotSortable
                .Columns.Add(Col3)

                Dim Col5 As New DataGridViewTextBoxColumn()
                'Col5.DataPropertyName = "UltimoContato"
                Col5.HeaderText = "Último"
                Col5.Width = 70
                Col5.Frozen = False
                Col5.ReadOnly = True
                Col5.FillWeight = 100
                Col5.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                Col5.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                Col5.ValueType = GetType(Date)
                Col5.Visible = True
                Col5.SortMode = DataGridViewColumnSortMode.NotSortable

                .Columns.Add(Col5)

                Dim Col6 As New DataGridViewTextBoxColumn()
                'Col6.DataPropertyName = "ProxContato"
                Col6.HeaderText = "Próximo"
                Col6.Width = 70
                Col6.Frozen = False
                Col6.ReadOnly = True
                Col6.FillWeight = 100
                Col6.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                Col6.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                Col6.ValueType = GetType(Date)
                Col6.SortMode = DataGridViewColumnSortMode.NotSortable
                Col6.Visible = True

                .Columns.Add(Col6)

                Dim ColHoraOculta As New DataGridViewTextBoxColumn
                'ColHoraOculta.DataPropertyName = "HoraOculta"
                ColHoraOculta.HeaderText = "HoraOculta"
                ColHoraOculta.Width = 70
                ColHoraOculta.Frozen = False
                ColHoraOculta.ReadOnly = True
                ColHoraOculta.FillWeight = 100
                ColHoraOculta.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                ColHoraOculta.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                ColHoraOculta.ValueType = GetType(DateTime)
                ColHoraOculta.Visible = False

                .Columns.Add(ColHoraOculta)

                Dim ColHora As New DataGridViewTextBoxColumn()
                'ColHora.DataPropertyName = "HoraOculta"
                ColHora.HeaderText = "Hora"
                ColHora.Width = 50
                ColHora.Frozen = False
                ColHora.ReadOnly = True
                ColHora.FillWeight = 100
                ColHora.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                ColHora.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                ColHora.ValueType = GetType(DateTime)
                ColHora.Visible = True
                ColHora.SortMode = DataGridViewColumnSortMode.NotSortable
                .Columns.Add(ColHora)

                Dim Col4 As New DataGridViewTextBoxColumn
                'Col4.DataPropertyName = "Tolerância"
                Col4.HeaderText = "Tolerância"
                Col4.Width = 70
                Col4.Frozen = False
                Col4.ReadOnly = True
                Col4.FillWeight = 100
                Col4.Visible = True
                Col4.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                Col4.SortMode = DataGridViewColumnSortMode.NotSortable
                .Columns.Add(Col4)

                Dim ColRegiao As New DataGridViewTextBoxColumn()
                'ColRegiao.DataPropertyName = "Região"
                ColRegiao.HeaderText = "Região"
                ColRegiao.Width = 70
                ColRegiao.Frozen = False
                ColRegiao.ReadOnly = True
                ColRegiao.FillWeight = 100
                ColRegiao.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                ColRegiao.Visible = True
                ColRegiao.SortMode = DataGridViewColumnSortMode.NotSortable

                .Columns.Add(ColRegiao)

                .Refresh()

            End With
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub

    Private Sub PreencherDtGrid()
        Dim idStatus As Int32
        Dim idRegiao As Int32
        Dim dtProxContato As Date
        Dim iTolerancia As Int32
        Dim iAtrasoContato As Int32
        Dim dtUltimoContato As String
        Dim dtHoraContato As Date
        Try
            dtClientes = objClientes.RetornarClientesProxDataContato(dtDataBase, False, 0)
            dtGridTarefas.Rows.Clear()
            If (Not dtClientes Is Nothing) Then
                With dtClientes

                    If (.Rows.Count > 0) Then
                        dtGridTarefas.Rows.Clear()
                        dtGridTarefas.RowCount = .Rows.Count

                        For cont As Integer = 0 To .Rows.Count - 1

                            dtGridTarefas.Item(eColTarefas.eIdCliente, cont).Value = .Rows(cont).Item("idCliente").ToString
                            dtGridTarefas.Item(eColTarefas.eNomeCliente, cont).Value = .Rows(cont).Item("RazaoSocial").ToString
                            idStatus = Persistencia.perConversao.nuloParaZero(.Rows(cont).Item("IDStatus").ToString)
                            dtGridTarefas.Item(eColTarefas.eIDStatus, cont).Value = idStatus
                            dtProxContato = Persistencia.perConversao.nuloParaData((.Rows(cont).Item("ProximoContato")))
                            If (dtProxContato <> Date.MinValue) Then
                                dtGridTarefas.Item(eColTarefas.eProximoContato, cont).Value = dtProxContato.ToShortDateString
                            End If

                            dtUltimoContato = objOcorrencias.RetornaUltimoContato(.Rows(cont).Item("idCliente")).Date

                            If Not (CDate(dtUltimoContato) = Date.MinValue) Then
                                dtGridTarefas.Item(eColTarefas.eUltimoContato, cont).Value = dtUltimoContato
                            Else
                                dtGridTarefas.Item(eColTarefas.eUltimoContato, cont).Value = "__/__/____"
                            End If

                            dtGridTarefas.Item(eColTarefas.eIdContato, cont).Value = .Rows(cont).Item("IDContato")
                            dtGridTarefas.Item(eColTarefas.eNomeContato, cont).Value = .Rows(cont).Item("NomeContato").ToString

                            iAtrasoContato = DateDiff(DateInterval.Day, dtProxContato, dtDataBase)

                            If (iAtrasoContato > 0) Then
                                If (iAtrasoContato > iTolerancia) Then
                                    dtGridTarefas.Rows(cont).DefaultCellStyle.ForeColor = Color.Red
                                ElseIf (iAtrasoContato <= iTolerancia) Then
                                    dtGridTarefas.Rows(cont).DefaultCellStyle.ForeColor = Color.Blue
                                End If
                            Else
                                dtGridTarefas.Rows(cont).DefaultCellStyle.ForeColor = Color.Black
                            End If
                            If (idStatus > 0) Then
                                If (idStatus = 99) Then
                                    dtGridTarefas.Rows(cont).DefaultCellStyle.ForeColor = Color.Green
                                Else
                                    dtGridTarefas.Item(eColTarefas.eStatus, cont).Value = Persistencia.perConversao.nuloParaVazio(.Rows(cont).Item("Descricao"))
                                    iTolerancia = Persistencia.perConversao.nuloParaZero(.Rows(cont).Item("Tolerancia"))
                                    If (iTolerancia > 0) Then
                                        dtGridTarefas.Item(eColTarefas.eTolerancia, cont).Value = DateAdd(DateInterval.Day, iTolerancia, dtProxContato).ToShortDateString
                                    End If
                                End If
                            End If
                            dtHoraContato = Persistencia.perConversao.nuloParaData(.Rows(cont).Item("Hora"))
                            If (dtHoraContato = Date.MinValue) Then
                                dtGridTarefas.Item(eColTarefas.eHora, cont).Value = ""
                                dtGridTarefas.Item(eColTarefas.eHoraOculta, cont).Value = Date.MaxValue
                            Else
                                dtGridTarefas.Item(eColTarefas.eHora, cont).Value = dtHoraContato.ToString("HH:mm")
                                dtGridTarefas.Item(eColTarefas.eHoraOculta, cont).Value = dtHoraContato.ToString("HH:mm")
                            End If
                            idRegiao = perConversao.nuloParaZero(.Rows(cont).Item("IdRegiao"))
                            If (idRegiao > 0) Then
                                dtGridTarefas.Item(eColTarefas.eRegiao, cont).Value = objRegiao.retornarCampoRegiao("Regiao", idRegiao).Item("Regiao").ToString
                            End If
                        Next
                    End If
                    dtGridTarefas.AllowUserToOrderColumns = False
                    dtGridTarefas.SelectionMode = DataGridViewSelectionMode.FullRowSelect
                    dtGridTarefas.Rows(0).Selected = False
                    dtGridTarefas.AllowUserToOrderColumns = False

                End With
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try
    End Sub

    Private Sub frmTarefas_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated

        'cmdFiltrar_Click(cmdFiltrar, EventArgs.Empty)

    End Sub

    Private Sub frmTarefas_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Call FormataDtGrid()
        dtDataBase = CDate(dateMin.Text)
        Call PreencherDtGrid()
        dateMin.Focus()

    End Sub

    Private Sub cmdSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSair.Click
        Me.Close()
    End Sub
    Private Sub dtGridTarefas_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtGridTarefas.CellDoubleClick
        Dim frm As New frmOcorrencia

        Try
            If (e.RowIndex >= 0) Then
                prvIDCliente = CLng(dtGridTarefas.Rows(e.RowIndex).Cells(eColTarefas.eIdCliente).Value.ToString())
                With frm
                    .IDCliente = prvIDCliente
                    .Origem = 1
                    If (.CarregaDados()) Then
                        .ShowDialog()
                    End If
                    cmdFiltrar_Click(cmdFiltrar, EventArgs.Empty)

                End With
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub

    Private Sub chkSomenteAtrasados_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkSomenteAtrasados.Click

        bSomenteAtrasados = (chkSomenteAtrasados.CheckState = CheckState.Checked)

    End Sub
    Private Sub cmdStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStatus.Click
        Dim sql As String
        Dim frmDialogo As New frmPesquisa

        sql = Me.objStatus.sqlConsulta

        With frmDialogo
            .Campos = "iIDStatus,sDescricao"
            .Descricoes = "Código,Status"
            .Larguras = "50,300"
            .Sql = sql
            .Titulo = "Pesquisa Status"
            .CarregaTela()
            If .DS.Tables(0).Rows.Count > 0 Then
                .ShowDialog()
                If (.DialogResult = Windows.Forms.DialogResult.OK) Then
                    Me.prvIDStatus = .Id
                    Me.txtStatus.Text = objStatus.retornarCampoStatus("Descricao", prvIDStatus).Item("Descricao").ToString
                End If
            Else
                MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                Me.prvIDStatus = 0
            End If
        End With
    End Sub

    Private Sub cmdFiltrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFiltrar.Click
        bSomenteAtrasados = (chkSomenteAtrasados.CheckState = CheckState.Checked)
        dtDataBase = CDate(dateMin.Text)
        Call PreencherDtGrid()
    End Sub

    Private Sub cmdLimparStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLimparStatus.Click
        prvIDStatus = 0
        txtStatus.Text = String.Empty
    End Sub

    Private Sub dtGridTarefas_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtGridTarefas.CellContentClick

    End Sub
End Class
